<template>
  <div class="main">
        <p class="main_text">商户消息管理</p>
      <!-- 搜索 -->
      <div class="g-search" ref="searchBox">
        <!-- 时间选择区域 -->
        <span class="demonstration">发送时间：</span>
        <el-date-picker v-model="searchValue.startTime" type="date" placeholder="开始时间" @change="startTime"
          :clearable="false" :default-time="defaultTime(0, 0, 0)" />
        <span class="demonstration"
          style="display: inline-block; font-size: 12px; color: #898989; margin: 20px">至</span>
        <el-date-picker v-model="searchValue.endTime" type="date" placeholder="结束时间" @change="endTime"
          :clearable="false" :default-time="defaultTime1(23, 59, 59)" />
        <el-radio-group v-model="radio" class="ml-4" @change="radioSelect" style="margin-left: 10px">
          <el-radio label="1" size="large">今天</el-radio>
          <el-radio label="2" size="large">近7天</el-radio>
          <el-radio label="3" size="large">近30天</el-radio>
          <el-radio label="4" size="large">近3个月</el-radio>
        </el-radio-group>
        <!-- 信息区域 -->
        <div class="all_select">
          <!-- 消息标题 -->
          <div class="input_valye">
            <span class="demonstration">消息标题</span>
            <input type="text" class="input" placeholder="请输入消息标题" v-model="searchValue.topic"  @keyup.enter="search" />
          </div>
          <!-- 店铺名称 -->
          <div class="input_valye">
            <span class="demonstration">店铺名称</span>
            <input type="text" class="input" placeholder="请输入店铺名称" v-model="searchValue.shopName"  @keyup.enter="search" />
          </div>
          <!-- 订单状态 -->
          <div class="input_valye">
            <span class="demonstration">店铺ID</span>
            <input type="text" class="input" placeholder="请输入店铺ID" v-model="searchValue.shopId"  @keyup.enter="search" />
          </div>
        </div>
        <!-- 底部按钮区域 -->
        <div class="bottom_btn">
          <el-button class="solid" @click="search">查询</el-button>
          <el-button class="hollow" @click="reset">重置</el-button>
        </div>
      </div>
      <!-- 表格 -->
      <div class="cyr-flex">
        <!-- 表格区域 -->
        <el-table :data="tableData"  @selection-change="handleSelectionChange"
          :header-cell-style="{ 'text-align': 'center' }"  :show-overflow-tooltip="true" v-adaptive border>
          <el-table-column type="selection" width="40" />
          <el-table-column prop="title" label="消息标题" align="center">
            <template #default="item">
              <el-text tag="span" style="cursor: pointer" @click="lookMes(item.row)">{{ item.row.title }}</el-text>
            </template>
          </el-table-column>
          <el-table-column prop="topic" label="推送模板" align="center" />
          <el-table-column prop="type" label="类型" align="center">
            <template #default="item">
              <el-text v-if="item.row.type == 0" class="mx-1" type="success">支付通知</el-text>
              <el-text v-if="item.row.type == 1" class="mx-1" type="default">物流通知</el-text>
              <el-text v-if="item.row.type == 2" class="mx-1" type="warning">平台公告</el-text>
              <el-text v-if="item.row.type == 3" class="mx-1" type="primary">优惠促销</el-text>
            </template>
          </el-table-column>

          <el-table-column prop="sendAt" label="发送时间" align="center" width="180" />
          <el-table-column label="操作" width="200" class="operation"  :show-overflow-tooltip="false">
            <template #default="item">
              <div class="table_btn">
                <el-button size="small" class="none" @click="lookMes(item.row)">查看</el-button>
                <!-- <el-button size="small" class="none" @click="DelMes(item.row)"
                                >删除</el-button
                            > -->
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <smartPager v-model:current-page="searchValue.page" v-model:page-size="searchValue.rows" :total="total"
          @change="pageChange" />
      </div>

      <!-- 查看信息 -->
      <div class="messages" v-if="messagesShow">
        <div class="title1">
          <div class="title_mes">
            <el-icon size="20" color="#fff">
              <Edit />
            </el-icon>&nbsp;群发消息
          </div>
          <span class="close" @click="maskBtn1">✖</span>
        </div>
        <div class="content1" style="margin-top: 40px; height: calc(100% - 170px); overflow-y: scroll">
          <el-form ref="ruleFormRef" :model="GoodsformDate" label-width="90px" label-position="right">
            <el-form-item label="消息标题:" prop="type">
              {{ GoodsformDate.title }}
            </el-form-item>
            <el-form-item label="推送类型:" prop="type">
              {{ GoodsformDate.type }}
            </el-form-item>
            <el-form-item label="消息模板:" prop="type">
              {{ GoodsformDate.templateId }}
            </el-form-item>
            <el-form-item label="消息内容:" prop="type">
              {{ GoodsformDate.content }}
            </el-form-item>
          </el-form>
        </div>
        <div class="btn_center">
          <el-button class="hollow" @click="plateformSure">取消</el-button>
          <el-button class="solid" @click="plateformSure">确定</el-button>
        </div>
      </div>
      <!-- 遮罩层 -->
      <div id="mask1" v-if="mask" @click="maskBtn1"></div>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted, reactive, onBeforeUnmount } from 'vue';
import { RequestPlatformToShop } from '@/api/path/index.js';
import moment from 'moment';
import { ElMessage } from 'element-plus';
import type { FormInstance } from 'element-plus';
import { Warning, Edit, User } from '@element-plus/icons-vue';
const ruleFormRef = ref<FormInstance>();

const defaultTime = (H: any, M: any, S: any) => {
  var now = new Date();
  now.setHours(H);
  now.setMinutes(M);
  now.setSeconds(S);
  return now;
};
const defaultTime1 = (H: any, M: any, S: any) => {
  var now = new Date();
  now.setHours(H);
  now.setMinutes(M);
  now.setSeconds(S);
  return now;
};
// 推送消息数据
const GoodsformDate = ref({
  templateId: '',
  type: '',
  content: '',
  title: '',
});
// 表单规则
const rules = reactive({
  type: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
  // unit: [{ required: true, message: '请选择商品单位', trigger: 'change' }],
  link: [{ required: true, message: '请选择商品分类', trigger: 'change' }],
});
const tableData = ref<any>([]); // 表格列表
const total = ref(0); //表格总数
const mask = ref(false); //控制遮罩层显示
const messagesShow = ref(false); //控制群发显示
const order = ref<any>([]); //订单数组
const radio = ref('');

// 搜索参数
const searchValue = ref({
  shopId: '',
  shopName: '',
  startTime: '',
  endTime: '',
  topic: '',
  page: 1,
  rows: 10,
});
/**
 * 查看信息
 */
const lookMes = (item: any) => {
  console.log('123,', item);
  GoodsformDate.value = {
    templateId: item.topic,
    type: item.type,
    content: item.content,
    title: item.title,
  };
  if (item.type == 0) {
    GoodsformDate.value.type = '支付通知';
  } else if (item.type == 0) {
    GoodsformDate.value.type = '物流通知';
  } else if (item.type == 0) {
    GoodsformDate.value.type = '平台公告';
  } else {
    GoodsformDate.value.type = '优惠促销';
  }
  messagesShow.value = true;
  mask.value = true;
};
/**
 * 查看消息确认
 */
const plateformSure = async () => {
  messagesShow.value = false;
  mask.value = false;
};
/**
 * 时间范围单选按钮
 */
const radioSelect = () => {
  searchValue.value.startTime = '';
  searchValue.value.endTime = '';
  var now = new Date();
  now.setHours(0);
  now.setMinutes(0);
  now.setSeconds(0);
  var now1 = new Date();
  now1.setHours(23);
  now1.setMinutes(59);
  now1.setSeconds(59);
  if (radio.value === '1') {
    searchValue.value.startTime = moment(now).format('YYYY-MM-DD HH:mm:ss');
    searchValue.value.endTime = moment(now1).format('YYYY-MM-DD HH:mm:ss');
  } else if (radio.value === '2') {
    searchValue.value.startTime = moment(now).subtract(7, 'days').format('YYYY-MM-DD HH:mm:ss');
    searchValue.value.endTime = moment(now1).format('YYYY-MM-DD HH:mm:ss');
  } else if (radio.value === '3') {
    searchValue.value.startTime = moment(now).subtract(30, 'days').format('YYYY-MM-DD HH:mm:ss');
    searchValue.value.endTime = moment(now1).format('YYYY-MM-DD HH:mm:ss');
  } else {
    searchValue.value.startTime = moment(now).subtract(3, 'months').format('YYYY-MM-DD HH:mm:ss');
    searchValue.value.endTime = moment(now1).format('YYYY-MM-DD HH:mm:ss');
  }
  search()
};
// 开始时间
const startTime = () => {
  searchValue.value.startTime.setHours(0);
  searchValue.value.startTime.setMinutes(0);
  searchValue.value.startTime.setSeconds(0);
  searchValue.value.startTime = moment(searchValue.value.startTime).format('yyyy-MM-DD HH:mm:ss');
  radio.value = '0';
  search()
};
// 结束时间
const endTime = () => {
  searchValue.value.endTime.setHours(23);
  searchValue.value.endTime.setMinutes(59);
  searchValue.value.endTime.setSeconds(59);
  searchValue.value.endTime = moment(searchValue.value.endTime).format('yyyy-MM-DD HH:mm:ss');
  radio.value = '0';
  search()
};
/**
 * 搜索
 */
const search = async () => {
  // 判断开始时间不能早于当前时间
  if (moment(searchValue.value.startTime).diff(moment().format('YYYY-MM-DD HH:mm:ss')) >= 0) {
    ElMessage.error('开始时间不能晚于当前日期');
    searchValue.value.startTime = '';
  } else {
    // 判断是否两个时间都填写了
    if (searchValue.value.startTime !== '' && searchValue.value.endTime !== '') {
      // 判断开始时间不能晚于结束时间
      if (moment(searchValue.value.startTime).diff(moment(searchValue.value.endTime)) <= 0) {
        searchValue.value.page = 1;
        requestPlatformToShop();
      } else {
        ElMessage.error('结束时间不能早于开始时间');
        searchValue.value.endTime = '';
      }
    } else {
      // 表示两个时间填写了任意一个
      searchValue.value.page = 1;
      requestPlatformToShop();
    }
  }
};
/**
 * 分页
 */
const pageChange = async (val: number) => {
  requestPlatformToShop()
};
// 挂载
onMounted(() => {
  requestPlatformToShop();
  handleResize();
  window.addEventListener('resize', handleResize);
});
const searchBox = ref();
const tablelistHeight = ref();
const handleResize = () => {
  const searchBoxHeight = searchBox.value.clientHeight;
  tablelistHeight.value = 110 + searchBoxHeight + 'px';
  console.log('Search Box height11111111111111111111111111111:', searchBoxHeight);
};
/**
 * 获取用户列表
 */
const requestPlatformToShop = async () => {
  const a = await RequestPlatformToShop(searchValue.value);
  total.value = a.resultObj.allCount;
  tableData.value = a.resultObj.shopMessages;
  console.log('获取成功', a);
};
/**
 * 点击重置
 */
const reset = () => {
  searchValue.value = {
    shopId: '',
    shopName: '',
    topic: '',
    startTime: '',
    endTime: '',
    page: 1,
    rows: 15,
  };
  requestPlatformToShop();
  radio.value = '0';
};
/**
 * 表格复选框选中事件
 */
const handleSelectionChange = (selection: any) => {
  order.value = [];
  for (let x = 0; x < tableData.value.length; x++) {
    tableData.value[x].selection = false;
  }
  for (let i = 0; i < selection.length; i++) {
    selection[i].selection = true;
    order.value.push(selection[i]);
  }
};

/**
 * 遮罩层数据
 */
const maskBtn1 = () => {
  mask.value = false;
  messagesShow.value = false;
};
</script>
<style lang="scss" scoped>
@import '@/assets/css/plateform/Article/Content.css';
@import '@/assets/css/plateform/Shop/GoodsManage.css';
.main{
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.table {
  height: calc(100% - v-bind(tablelistHeight)) !important;
}

::v-deep .el-table--fit {
  height: calc(100% - 70px) !important;
}

.table :deep(.el-table__cell) {
  text-align: center;
}

.nav1 {
  display: flex;
  height: 30px;
  align-items: center;
  margin-top: 20px;
  border-bottom: 0;

  .nav_li {
    width: 50%;
    height: 30px;
    line-height: 30px;
    text-align: center;
    font-size: 14px;
  }

  .current {
    border-bottom: 2px solid #5697f9;
  }
}

.table {
  overflow: hidden;
  padding: 0 10px;

  .table_function {
    margin-top: 20px;
  }
}

.el-form-item :deep(.el-input-group__prepend) {
  width: 10px;
  background-color: #116cf8;
  color: #fff;
}

.el-form-item :deep(.el-upload--picture-card) {
  height: 100px;
  width: 100px;
  opacity: 0;
}

.btn {
  width: 150px;
  position: absolute;
  right: 20px;
  bottom: 40px;
}

// 推送消息
.plateform,
.messages {
  width: 600px;
  height: 500px;
  position: absolute;
  position: fixed;
  top: 50%;
  left: 50%;
  margin-left: -300px;
  margin-top: -250px;
  z-index: 99;
  background: #fff;
}

.messages {
  height: 400px;
  margin-top: -200px;
}

:deep(.el-input__wrapper) {
  width: 150px;
}

:deep(.el-drawer.rtl) {
  background: #fff;
  width: 750px !important;
}

:deep(.el-overlay) {
  z-index: 40 !important;
  padding: static;
}

:deep(.is-process) {
  color: #409eff;
}

:deep(.is-process .el-step__icon.is-text) {
  border: 2px solid #409eff;
  background: #409eff;
  color: #fff;
}

:deep(.is-finish .el-step__line) {
  background: #409eff;
}

.el-table .warning-row {
  background: #44a2ff !important;
}

:deep(.el-select) {
  width: 100%;
}
</style>
